<template>
  <!-- :collapse-transition="false"：禁用菜单展开/折叠时的动画过渡效果。 -->
  <el-menu
    :width="width"
    :collapse="isCollapse"
    active-text-color="#ffd04b"
    background-color="#545c64"
    class="aside-container"
    text-color="#fff"
    :collapse-transition="false"
    :default-active="activeMenu"
    v-model:opened="openedMenus"
  >
    <h3 class="logo-lg" v-show="!isCollapse">V3陪诊</h3>
    <h3 class="logo-lg" v-show="isCollapse">V3</h3>
    <!-- <tree-menu :menuData="menuData" /> -->
    <el-menu-item
      v-for="item in noChildren"
      :index="item.name"
      :key="item.path"
      @click="handleMenuClick(item)"
    >
      <el-icon size="20">
        <component :is="item.meta.icon"></component>
      </el-icon>
      <span>{{ item.meta.name }}</span>
    </el-menu-item>
    <el-sub-menu
      v-for="item in hasChildren"
      :index="item.name"
      :key="item.path"
    >
      <template #title>
        <el-icon size="20">
          <component :is="item.meta.icon"></component>
        </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item
          v-for="(subItem, subIndex) in item.children"
          :index="subItem.name"
          :key="subItem.path"
          @click="handleMenuClick(subItem)"
        >
          <el-icon size="20">
            <component :is="subItem.meta.icon"></component>
          </el-icon>
          <span>{{ subItem.meta.name }}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-sub-menu>
  </el-menu>
</template>

<script setup>
import TreeMenu from "@/components/treeMenu.vue";
import { ref, reactive, computed, onMounted } from "vue";
import { useAllMenuDataStore } from "@/store/menu.js";
import { useRoute, useRouter } from "vue-router";

const route = useRoute();
const router = useRouter();
const menuStore = useAllMenuDataStore();
const isCollapse = computed(() => menuStore.state.isCollapse);
// width 宽度
const width = computed(() => (menuStore.state.isCollapse ? "64px" : "180px"));
const activeMenu = computed(() => {
  return route.name;
});
// 添加 openedMenus 的响应式定义
const openedMenus = ref([]);
const menuList = computed(() => menuStore.state.menuList);
const noChildren = computed(() =>
  menuList.value.filter((item) => !item.children)
);
const hasChildren = computed(() =>
  menuList.value.filter((item) => item.children)
);
const handleMenuClick = (item) => {
  router.push({
    name: item.name,
  });
  menuStore.selectMenu(item);
};
</script>

<style scoped lang="less">
.aside-container {
  height: 100%;
  .logo-lg {
    font-size: 20px;
    text-align: center;
    height: 50px;
    line-height: 50px;
    color: #fff;
  }
}
</style>
